package com.weiwei.diancan.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.weiwei.diancan.R;


/**
 * Conpany:成都欧品在线电子商务有限公司技术服务中心
 * Auther:孙福义
 * Date: 2016/2/19
 * Effect: layout界面状态自定义布局
 */
public class EmptyLayout extends LinearLayout implements View.OnClickListener {
    private Context context;
    private RelativeLayout mLayout; // error布局
    private ImageView img;    //图片
    private TextView tv;  //文字
    private int mErrorState;  //状态码
    private ProgressBar animProgress;  //进度条
    private boolean clickEnable = true;  //是否点击
    private OnClickListener listener;   //点击监听
    private String strNoDataContent = "";
    private LinearLayout shopcar_view, goods_error_view;
    private TextView goods_error;
    private Button go_shopping, my_coll;
    public static final int HIDE_LAYOUT = 4; //正常情况下
    public static final int NETWORK_ERROR = 1; //没有网络时
    public static final int NETWORK_LOADING = 2;//网络加载时
    public static final int NO_DATA = 3;      //没有数据时
    public static final int NO_DATA_ENABLE_CLICK = 5; //没数据重复点击时
    public static final int NO_LOGIN = 6;    //没有登录时

    public static final int SHOP_CAT = 7; //购物车
    public static final int MESSAGE = 8; //消息
    public static final int NOT_DATA = 9; //无数据
    public static final int COLLECTION = 10; //收藏
    public static final int COUPON = 11; //优惠券
    public static final int GOODS = 12;//商品
    public static final int ADDRESS = 13;//地址

    public static final int NEWPREFENTAIL = 14;//特惠上新
    public static final int WULIU = 15; //物流信息
    public static final int BANLANCE = 16;//充值记录
    private boolean isList;
    private boolean isGoods;
    private String goodsCategroy = "";
    private TextView tv_notext;
    private TextView tv_notext_1;
    private TextView tv_notext_2;

    public String getGoodsCategroy() {
        return goodsCategroy;
    }

    public void setGoodsCategroy(String goodsCategroy) {
        this.goodsCategroy = goodsCategroy;
    }

    public boolean isGoods() {
        return isGoods;
    }

    public void setIsGoods(boolean isGoods) {
        this.isGoods = isGoods;
    }

    public EmptyLayout(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public EmptyLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    public boolean isList() {
        return isList;
    }

    public void setIsList(boolean isList) {
        this.isList = isList;
    }

    private void init() {
        View v = View.inflate(context, R.layout.error_layout, null);
        tv = (TextView) v.findViewById(R.id.tv_error_layout);
        img = (ImageView) v.findViewById(R.id.img_error_layout);
        tv_notext = (TextView) v.findViewById(R.id.tv_notext);
        tv_notext_1 = (TextView) v.findViewById(R.id.tv_notext_1);
        tv_notext_2 = (TextView) v.findViewById(R.id.tv_notext_2);
        mLayout = (RelativeLayout) v.findViewById(R.id.pageerrLayout);
        animProgress = (ProgressBar) v.findViewById(R.id.animProgress);
        shopcar_view = (LinearLayout) v.findViewById(R.id.shopcar_view);
        go_shopping = (Button) v.findViewById(R.id.go_shopping);
        my_coll = (Button) v.findViewById(R.id.my_coll);
        goods_error_view = (LinearLayout) v.findViewById(R.id.goods_error_view);
        goods_error = (TextView) v.findViewById(R.id.goods_error);
        setBackgroundColor(Color.TRANSPARENT);
        mLayout.setOnClickListener(this);
        img.setOnClickListener(this);
        go_shopping.setOnClickListener(this);
        my_coll.setOnClickListener(this);

        addView(v);

    }

    //隐藏
    public void dismiss() {
        mErrorState = HIDE_LAYOUT;
        setVisibility(View.GONE);
    }


    //获取错误状态
    public int getErrorState() {
        return mErrorState;
    }

    //加载出错
    public boolean isLoadError() {
        return mErrorState == NETWORK_ERROR;
    }

    //正在加载
    public boolean isLoading() {
        return mErrorState == NETWORK_LOADING;
    }

    //设置错误信息，tv展示
    public void setErrorMessage(String msg) {
        tv.setText(msg);
    }

    public void setErrorType(int state, int type) {
        setVisibility(View.VISIBLE);
        tv.setVisibility(View.VISIBLE);
        switch (state) {
            case NETWORK_ERROR:
                //网络错误状态
                setBackgroundColor(-1);
                mErrorState = NETWORK_ERROR;
                tv.setText(R.string.error_network);
                img.setBackgroundResource(R.mipmap.wangluo_fl);
                img.setVisibility(View.VISIBLE);
                animProgress.setVisibility(View.GONE);
                clickEnable = true;
                break;
            case NETWORK_LOADING:
                if (isList) {
                    //正在加载状态...
                    setBackgroundColor(Color.TRANSPARENT);
                } else {
                    setBackgroundColor(-1);
                }
                tv.setVisibility(View.GONE);
                strNoDataContent = "";
                mErrorState = NETWORK_LOADING;
                animProgress.setVisibility(View.VISIBLE);
                goods_error_view.setVisibility(View.GONE);
                img.setVisibility(View.GONE);
                clickEnable = false;
                break;
            case NO_DATA:
                //没数据状态
                setBackgroundColor(-1);
                switch (type) {
//                    case SHOP_CAT:
//                        img.setBackgroundResource(R.mipmap.gouwuche_gwc);
//                        shopcar_view.setVisibility(View.VISIBLE);
//                        img.setVisibility(View.VISIBLE);
//                        setNoDataContent(getResources().getString(R.string.shopcart_no_data));
//                        setTvNoDataContent();   //暂无数据
//                        break;
//                    case MESSAGE:
//                        img.setVisibility(View.VISIBLE);
//                        img.setBackgroundResource(R.mipmap.xiaoxi_xx);
//                        strNoDataContent = getResources().getString(R.string.message);
//                        setTvNoDataContent();   //暂无数据
//                        break;
                    case NOT_DATA:
                        img.setVisibility(View.VISIBLE);
                        img.setBackgroundResource(R.mipmap.page_icon_empty);
                        strNoDataContent = getResources().getString(R.string.error_no_data);
                        setTvNoDataContent();   //暂无数据
                        break;
//                    case COLLECTION:
//                        img.setVisibility(View.VISIBLE);
//                        img.setBackgroundResource(R.mipmap.shoucang_wdsc);
//                        strNoDataContent = getResources().getString(R.string.collection_no_data);
//                        setTvNoDataContent();   //暂无数据
//                        break;
//                    case COUPON:
//                        img.setVisibility(View.VISIBLE);
//                        img.setBackgroundResource(R.mipmap.youhuiquan_wdyhj);
//                        strNoDataContent = getResources().getString(R.string.coupon_no_data);
//                        setTvNoDataContent();   //暂无数据
//                        break;
//                    case GOODS:
//                        img.setVisibility(View.GONE);
//                        tv.setVisibility(View.GONE);
//                        goods_error_view.setVisibility(View.VISIBLE);
//                        setBackgroundColor(context.getResources().getColor(R.color.bg_content_color));
//                        if (getGoodsCategroy().equals("")) {
//                            goods_error.setText("符合条件");
//                        } else {
//                            goods_error.setText("'" + getGoodsCategroy() + "'");
//                            goods_error.setTextColor(context.getResources().getColor(R.color.home_recommend_goods_tv_sales_price));
//                        }
//                        break;
//                    case BANLANCE:
//                        img.setVisibility(View.GONE);
//                        tv.setVisibility(View.GONE);
//                        goods_error_view.setVisibility(View.VISIBLE);
//                        setBackgroundColor(context.getResources().getColor(R.color.bg_content_color));
//                        goods_error.setText("抱歉！您还没相关交易记录");
//                        tv_notext.setVisibility(View.GONE);
//                        tv_notext_1.setVisibility(View.GONE);
//                        tv_notext_2.setVisibility(View.GONE);
//                        break;
//                    case ADDRESS:
//                        img.setVisibility(View.VISIBLE);
//                        img.setBackgroundResource(R.mipmap.dizhi_shdz);
//                        strNoDataContent = getResources().getString(R.string.address_no_data);
//                        setTvNoDataContent();   //暂无数据
//                        break;
//                    case WULIU:
//                        img.setVisibility(View.VISIBLE);
//                        img.setBackgroundResource(R.mipmap.wuliu_ckwl);
//                        strNoDataContent = getResources().getString(R.string.wuliu_no_data);
//                        setTvNoDataContent();   //暂无数据
//                        break;
//                    case NEWPREFENTAIL:
//                        img.setVisibility(View.GONE);
//                        tv.setVisibility(View.GONE);
//                        goods_error_view.setVisibility(View.VISIBLE);
//                        setBackgroundColor(context.getResources().getColor(R.color.bg_content_color));
//                        goods_error.setText("抱歉！没有找到相关商品,\n换个搜索词再试试吧！");
//                        break;
                }

                animProgress.setVisibility(View.GONE);
                clickEnable = true;
                break;
            case HIDE_LAYOUT:
                //网络加载成功,有数据时状态
                setVisibility(View.GONE);
                break;
            case NO_DATA_ENABLE_CLICK:
                setBackgroundColor(-1);
                //网络加载失败,无数据时重新点击状态
                mErrorState = NO_DATA_ENABLE_CLICK;
                img.setBackgroundResource(R.mipmap.page_icon_empty);
                // img.setBackgroundDrawable(SkinsUtil.getDrawable(context,"page_icon_empty"));
                img.setVisibility(View.VISIBLE);
                animProgress.setVisibility(View.GONE);
                setTvNoDataContent();
                clickEnable = true;
                break;
            default:
                break;
        }
    }

    //无数据时显示的文字
    public void setNoDataContent(String noDataContent) {
        strNoDataContent = noDataContent;
    }

    public void setTvNoDataContent() {
        if (!strNoDataContent.equals("")) {
            tv.setText(strNoDataContent);
        } else {
            tv.setText(R.string.error_no_data);
        }
    }

    public void setOnLayoutClickListener(OnClickListener listener) {
        this.listener = listener;
    }

    public OnShopCarButtonClickListener setOnLeftButtonClickListener;

    public void OnShopCarButtonClickListener(OnShopCarButtonClickListener setOnLeftButtonClickListener) {
        this.setOnLeftButtonClickListener = setOnLeftButtonClickListener;
    }

    public interface OnShopCarButtonClickListener {
        void setLeftButtonClick(int index);
    }


    @Override
    public void onClick(View v) {
        if (v == mLayout) {
            if (clickEnable) {
                if (listener != null) {
                    listener.onClick(v);
                }
            }
        } else if (v == go_shopping) {
            if (setOnLeftButtonClickListener != null) {
                setOnLeftButtonClickListener.setLeftButtonClick(1);
            }
        } else if (v == my_coll) {
            if (setOnLeftButtonClickListener != null) {
                setOnLeftButtonClickListener.setLeftButtonClick(2);
            }
        }

    }
}
